package main

import (
	"database/sql"

	_ "github.com/go-sql-driver/mysql"
)

var MysqlDb *sql.DB
var MysqlDbErr error

const (
	USER_NAME = "root"
	USER_PWD  = "test_1388"
	USER_HOST = "127.0.0.1"
	USER_PORT = "3306"
	USER_DB   = "shop_db"
)

func main() {
	println("mysqldemo")
	defer MysqlDb.Close()
	sql := "select uid,username,password,email from users"
	rows, err := MysqlDb.Query(sql)
	if err != nil {
		panic(err)
	}
	defer rows.Close()
	for rows.Next() {
		var uid int
		var userName string
		var email string
		var password string
		err := rows.Scan(&uid, &userName, &password, &email)
		if err != nil {
			panic(err)
		}
		println(uid, userName, email, password)
	}
}

func init() {
	MysqlDb, MysqlDbErr = sql.Open("mysql", USER_NAME+":"+USER_PWD+"@tcp("+USER_HOST+":"+USER_PORT+")/"+USER_DB+"?charset=utf8")
	if MysqlDbErr != nil {
		panic(MysqlDbErr)
	}
	MysqlDb.SetMaxOpenConns(200)
	MysqlDb.SetMaxIdleConns(16)
	MysqlDbErr = MysqlDb.Ping()
	if MysqlDbErr != nil {
		panic(MysqlDbErr)
	}
	println("mysql connect success")
}
